<template>
  <div class="login">
    <div class="container">
      <h2>登录</h2>
      <el-form
        class="form"
        ref="loginForm"
        :model="loginForm"
        :rules="rules"
        label-width="60px"
        hide-required-asterisk >
        <el-form-item prop="username">
          <span slot="label">用户名</span>
          <el-input v-model="loginForm.username" placeholder="请输入用户名"/>
        </el-form-item>
        <el-form-item prop="password">
          <span slot="label">密码</span>
          <el-input v-model="loginForm.password" placeholder="请输入密码" show-password/>
        </el-form-item>
        <el-form>
          <el-button type="primary" @click="handleLogin" :loading="isButtonLoading" plain>登录</el-button>
          <el-button type="success" @click="goRegister" plain>立即注册</el-button>
        </el-form>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: 'test',
        password: '123456'
      },
      rules: {
        username: [
          {
            message: '请输入用户名',
            required: true,
            trigger: 'blur'
          }
        ],
        password: [
          {
            message: '请输入密码',
            required: true,
            trigger: 'blur'
          }
        ]
      },
      isButtonLoading: false
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.isButtonLoading = true
          this.$store.dispatch('user/login', this.loginForm).then(res => {
            this.isButtonLoading = false
            this.$router.replace({
              path: '/dashboard'
            })
          }, err => {
            this.isButtonLoading = false
            console.log(err)
          })
        }
      })
    },
    goRegister() {
      this.$router.push({
        path: '/register'
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  background: url("../..//assets/images/background.jfif") no-repeat;
  background-size: cover;
  background-attachment: fixed;
  .container {
    width: 500px;
    border-radius: 5px;
    padding: 0 30px 30px 30px;
    background-color: #ffffff7d;
    box-shadow: 0 0 10px #bebebe;
    .form {
      span {
        color: white;
      }
    }
    h2 {
      text-align: center;
      margin: 20px 0;
    }
    .el-button {
      width: 48%;
    }
  }
}
</style>
